package blazingbubble.gui.elements;



import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

import blazingbubble.gui.Resources;


public class MenuTextField extends JTextField implements KeyListener {
	private static final long serialVersionUID = 3956579331048520520L;
	
	private Image backgroundImage = null;
/*	private class JTextFieldLimit extends PlainDocument {
		/*
		 * Private helper class to enforce possible character limit
		 
		private static final long serialVersionUID = -1988223357824220887L;
		private int limit;

		public JTextFieldLimit(int limit) {
			super();
			this.limit = limit;
		}

		@Override
		public void insertString(int offset, String  str, AttributeSet attr) throws BadLocationException {
			if (str == null){
				return;
			}
			if ((getLength() + str.length()) <= limit) {
				super.insertString(offset, str, attr);
			}
		}
	}
*/
	public MenuTextField(String backgroundImagePath, int characterLimit, int fontStyle, float fontSize){
		this.addKeyListener(this);
		this.setOpaque(false);

		Font font = null;
		FileInputStream inputStream = null;

		try {
			this.backgroundImage = ImageIO.read(new File(backgroundImagePath));
			inputStream = new FileInputStream(new File((Resources.CRAYON_FONT_PATH)));
			font = (Font.createFont(Font.TRUETYPE_FONT, inputStream).deriveFont(fontStyle, fontSize));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FontFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			if (inputStream != null){
				try {
					inputStream.close();
				}
				catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		this.setHorizontalAlignment(JTextField.CENTER);
		this.setFont(font);
		this.setForeground(Color.BLACK);
	//	if (characterLimit > 0){
	//		this.setDocument(new JTextFieldLimit(characterLimit));
	//	}		
	}
	
	@Override
	public void paint(Graphics graphics){
		super.paint(graphics);
		graphics.drawImage(backgroundImage, 0, 0, null);
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		if(arg0.getKeyCode() == 8) {
			int cp = this.getCaretPosition();
			if(cp > 0 && cp < getText().length() + 1) {
				StringBuffer t = new StringBuffer(getText());
				t.deleteCharAt(cp - 1);
				this.setText(t.toString());
				this.setCaretPosition(cp - 1);
			}
		}
		else if (arg0.getKeyCode() == 127){
			int cp = this.getCaretPosition();
			if (cp >= 0 && cp < getText().length()){
				StringBuffer t = new StringBuffer(getText());
				t.deleteCharAt(cp);
				this.setText(t.toString());
				this.setCaretPosition(cp);
			}
		}
		else if (arg0.getKeyCode() == 37){
			if(this.getCaretPosition() > 0){
				this.setCaretPosition(this.getCaretPosition() - 1);
			}
		}
		else if (arg0.getKeyCode() == 39){
			if(this.getCaretPosition() < this.getText().length()){
				this.setCaretPosition(this.getCaretPosition() + 1);
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
